import java.util.Arrays;
import java.util.List;

import org.apache.commons.compress.utils.Lists;

import com.alibaba.excel.EasyExcel;

public class Test1
{
	public static void main(String[] args)
	{
		new Test1().dynamicHeadWrite();
	}
	
	public void dynamicHeadWrite()
	{
		String fileName = "dynamicHeadWrite" + System.currentTimeMillis() + ".xlsx";
		EasyExcel.write(fileName)
				.head(head())
				.sheet("模板")
				.doWrite(contentData());
	}
	
	private static List<List<String>> head()
	{
		List<List<String>> headTitles = Lists.newArrayList();
		String basicInfo = "基础资料", skuInfo = "商品扩展", orderInfo = "经营情况", empty = " ";
		// 第一列，1/2/3行
		headTitles.add(Arrays.asList(basicInfo, basicInfo, "类别"));
		// 第二列，1/2/3行
		headTitles.add(Arrays.asList(basicInfo, basicInfo, "名称"));
		List<String> skuTitles = Arrays.asList("组合商品", "上一次优惠时间", "销售次数", "库存", "价格");
		skuTitles.forEach(title -> {
			headTitles.add(Arrays.asList(skuInfo, skuInfo, title));
		});
		List<Integer> monthList = Arrays.asList(5, 6);
		// 动态根据月份生成
		List<String> orderSpeaces = Arrays.asList("销售额", "客流", "利润");
		monthList.forEach(month -> {
			orderSpeaces.forEach(title -> {
				headTitles.add(Arrays.asList(orderInfo, month + "月", title));
			});
		});
		// 无一、二行标题
		List<String> lastList = Arrays.asList("日均销售金额(元)", "月均销售金额(元)");
		lastList.forEach(title -> {
			headTitles.add(Arrays.asList(empty, empty, title));
		});
		
		System.out.println(headTitles);
		return headTitles;
	}

	private static List<List<Object>> contentData()
	{
		List<List<Object>> contentList = Lists.newArrayList();
		// 这里一个List<Object>才代表一行数据，需要映射成每行数据填充，横向填充（把实体数据的字段设置成一个List<Object>）
		contentList.add(Arrays.asList("测试", "商品A", "苹果🍎"));
		contentList.add(Arrays.asList("测试", "商品B", "橙子🍊"));
		return contentList;
	}
}
